﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Web;
using System.Net;
using System.Configuration;
using System.Collections.Specialized;
using System.Net.Mail;
using System.Globalization;

namespace Dln.Common
{
    public class CommonHelper
    {
        #region 配置文件
        public static String GetAppSettings(String KeyName)
        {
            return ConfigurationManager.AppSettings[KeyName] != null ? ConfigurationManager.AppSettings[KeyName].ToString() : "";
        }
        #endregion
        #region 生成随机号
        /// <summary>
        /// 生成随机号
        /// </summary>
        /// <param name="length">长度</param>
        /// <returns></returns>
        public static string GenRandomNumber(int length)
        {
            string[] source = null;
            string code = "";
            source = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            Random rd = new Random();
            for (int i = 0; i < length; i++)
            {
                code += source[rd.Next(0, source.Length)];
            }
            return code;
        }
        #endregion

        /// <summary>
        /// 插入测试消息记录
        /// </summary>
        /// <param name="info"></param>
        public static void InsertTextMessage(string info)
        {
        }

        #region DataGridParam
        /// <summary>
        /// 获取post传值，并返回DataGridParam类型
        /// </summary>
        /// <param name="postValueCollection"></param>
        /// <returns></returns>
        public static DataGridParam GetDataGridParam(NameValueCollection postValueCollection)
        {
            var paramValues = new DataGridParam();
            paramValues.Page = Convert.ToInt32(postValueCollection["Page"].Split(',')[0]);
            paramValues.RP = Convert.ToInt32(postValueCollection["RP"]);
            paramValues.SortName = postValueCollection["sort"];
            paramValues.SortOrder = postValueCollection["order"];
            if (!string.IsNullOrEmpty(postValueCollection["Query"]))
            {
                paramValues.Query = postValueCollection["Query"];
            }
            return paramValues;
        }
        #endregion

        #region 将String转换为Dictionary类型
        /// <summary>
        /// 将String转换为Dictionary类型，过滤掉为空的值，首先 6 分割，再 7 分割:&
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Dictionary<string, string> StringToDictionary(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                Dictionary<string, string> queryDictionary = new Dictionary<string, string>();
                string[] s = value.Split('^');
                for (int i = 0; i < s.Length; i++)
                {
                    if (!string.IsNullOrWhiteSpace(s[i]) && !s[i].Contains("undefined") && !s[i].Contains("null"))
                    {
                        var ss = s[i].Split('&');
                        if ((!string.IsNullOrEmpty(ss[0])) && (!string.IsNullOrEmpty(ss[1])))
                        {
                            queryDictionary.Add(ss[0], ss[1]);
                        }
                    }

                }
                return queryDictionary;
            }
            return null;
        }
        #endregion

        #region 验证是否包含中文
        public static bool IsNumeric(string value)
        {
            return Regex.IsMatch(value, @"[\u4e00-\u9fa5]");
        }
        #endregion



    }
}
